# -*- coding: UTF-8 -*-
'''
@Project ：suanfa 
@File    ：链表.py
@IDE     ：PyCharm 
@Author  ：Wu
@Date    ：2023/12/18 15:26 
'''

# 定义Node
# 链表中的节点，每个节点包括一个数据项和一个指向下一个节点的指针
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

# 链表
# 包含一个头节点和一些操作方法
class LinkedList:
    def __init__(self):
        self.head = None

    # 往链表添加数据
    def append(self, data):
        new_node = Node(data) # new_code为Node对象 当前节点数据和指向下一节点(next)指针None
        # 如果头节点为空，把data赋值给头节点
        # self.data = 1 self.next = None
        if self.head is None:
            self.head = new_node
        # 假如第二个节点  2

        else:
            # current 赋值为self.head (self.data = 1 self.next = None)
            # 1:self.head(self.data=1,self.next=None)
            # 2:self.head(self.data=1,self.next=(self.data=2,self.next=None))
            # 3:self.head(self.data=1,self.next=(self.data=2,self.next=(self.data=3,self.next=None)))
            current = self.head
            # 如果next有值，继续找一个节点，直到下一节点为None为止
            while current.next:
                current = current.next
            # 把下一个节点赋值 new_code（Node对象 当前节点数据和指向下一节点(next)指针None）
            current.next = new_node

    def delete(self, data):
        if self.head is None:
            return

        if self.head.data == data:
            self.head = self.head.next
            return

        current = self.head
        while current.next:
            if current.next.data == data:
                current.next = current.next.next
                return
            current = current.next

    def display(self):
        current = self.head
        while current:
            print(current.data, end=" ")
            current = current.next

    def reverse(self):
        prev = None
        current = self.head
        while current:
            # 把当前节点的指向下一节点的指针赋值给next
            next = current.next
            # prev 当做反转链表的下一节点的指针指向，完成反转操作
            current.next = prev
            # 存储下一次循环需要的下一节点的指针指向数据
            prev = current
            # 下次循环从下一个节点开始
            current = next
        self.head = prev

    """
        def reverse(self):
            prev = None
            current = self.head
            # 1. 1,2,3,4,None
            # 2. 2,3,4,None
            # 3. 3,4,None
            # 4. 4,None
            while current:
                next = current.next
                # 1== 2,3,4,None
                # 2== 3,4,None
                # 3== 4,None
                # 4== None
                current.next = prev
                # 1 current 1,None
                # 2 current 2,1,None
                # 3 current 3,2,1,None
                # 4 current 4,3,2,1,None
                prev = current
                # 1 prev 1,None
                # 2 prev 2,1,None
                # 3 prev 3,2,1,None
                # 4 prev 4,3,2,1,None
                current = next
                # 1 current 2,3,4,None
                # 2 current 3,4,None
                # 3 current 4,None
                # 4 current None
            # self.head = prev
    """
# 创建链表
my_list = LinkedList()
my_list.append(1)
my_list.append(2)
my_list.append(3)
my_list.append(4)


# 删除指定元素
# my_list.delete(2)

# 打印链表
my_list.display()

my_list.reverse()
my_list.display()